<?php
class Admin_DocumentsController extends Zend_Controller_Action
{	
    public function init() {
        if (Zend_Auth::getInstance ( 'admin' )->setStorage ( new Zend_Auth_Storage_Session ( 'admin' ) )->hasIdentity () == false) {
			$this->_helper->getHelper ( 'Redirector' )->gotoUrl ( "admin/login" );
		} 
    }
    public function indexAction()
    {
      $this->view->title = "Tài liệu";	
    	try {
            
            $model = new Vina_Model_Documents();
            $dataList = $model->fetchListToArray("1=1", 'id desc');
            $paginator         = Zend_Paginator::factory($dataList);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(10);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
            
            $this->view->data = $paginator;     
                
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    
    }
    
    public function addAction() {
        try {
           $model = new Vina_Model_Documents();
           $form                   = new Admin_Form_Document();
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   $model->setTypeDocId($dataInsert['type_doc_id']);
                   $model->setTypeFoderId($dataInsert['type_foder_id']);
                   $model->setWhereId($dataInsert['where_id']);
                   $model->setTitle($dataInsert['title']);
                   $model->setKeyNumber($dataInsert['key_number']);
                   $model->setLink($dataInsert['link']);
                   $model->setDate(strtotime($dataInsert['date']));
                   $model->save();
                   $this->_redirect('/admin/documents');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    public function editAction() {
        try {
           $model = new Vina_Model_Documents();
           $form                   = new Admin_Form_Document();
           $form->getElement('link')->setRequired(false);
           $form->getElement('key_number')->removeValidator('Db_NoRecordExists');

           $id                     =  $this->_request->getParam('id',false);
           $data = $model->find($id)->toArray();
           $data['date'] = date('Y-m-d',$data['date']);
           $form->populate($data);
            if ($this->_request->isPost()) {
                
                $formData = $this->_request->getPost();

                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   $model->setTypeDocId($dataInsert['type_doc_id']);
                   $model->setTypeFoderId($dataInsert['type_foder_id']);
                   $model->setWhereId($dataInsert['where_id']);
                   $model->setTitle($dataInsert['title']);
                   $model->setKeyNumber($dataInsert['key_number']);
                   $model->setLink($dataInsert['link']);
                   $model->setDate(strtotime($dataInsert['date']));
                   $model->save();
                   $this->_redirect('/admin/documents');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
  public function deleteAction() {
     $id                     =  $this->_request->getParam('id',false);
     $news = new Vina_Model_News();
     $where = "id = $id";
     $news->delete($where);
     $this->_redirect($_SERVER['HTTP_REFERER']);
  }
    	
}
